#include "cacsiagent.h"
#if defined(_WINDOWS) || defined(WIN32)

#else
#include        "acsi_class.inl"
#include <netdb.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include        "ns_stat.h"
#endif
CAcsiAgent::CAcsiAgent(void)
{
    if(Acsi_Init() < 0)
    {
        printf("\nAcsi_Init() err\n");
        return;
    }
    m_ServerModel = new CServerModel;
    if (!m_ServerModel)
    {
        printf("Load CServerModel Error\n");
        return;
    }
    m_ServiceGetRCB=new CServiceGetRCB;
    if (!m_ServiceGetRCB)
    {
        printf("Load CServiceGetRCB Error\n");
        return;
    }
    m_DevIsAssociated = FALSE;
    m_AppIsConnected = FALSE;
}

CAcsiAgent::~CAcsiAgent()
{
    //if (m_ModelXml)
    //{
    //    delete m_ModelXml;
    //    m_ModelXml=NULL;
    //}
    if (m_ServerModel)
    {
        delete m_ServerModel;
        m_ServerModel=NULL;
    }
}
BOOL CAcsiAgent::AssociateDevice(const char * szAccessPoint,
                                 const char * szIpAddr)
{
    SetTimer(5);
    int ret = Associate(szAccessPoint, szIpAddr);
    if (ret > 0)
    {
        m_DevIsAssociated = TRUE;
        return TRUE;
    }
    else
    {
        m_DevIsAssociated = FALSE;
        return FALSE;
    }
}
BOOL CAcsiAgent::SetXml(const char* filename)
{
    //m_ModelXml=new CXmlRW(filename);
    //if (!m_ModelXml)
    //{
    //    return FALSE;
    //}
    //return TRUE;
}
BOOL CAcsiAgent::OnGetServerDirectory(int iClass, CMTree & tree)
{
    return m_ServerModel->ProcGetServerDirectory(iClass, tree);
}
void CAcsiAgent::GetServerModel(QTreeWidgetItem *rootItem)
{
    QStringList ServerDirectoryList, LogicDeviceDirectoryList,
                LogicNodeDirectoryList;
    QStringList xmlAttr,xmlAttrValue;
    ServerDirectoryList.clear();
    LogicDeviceDirectoryList.clear();
    LogicNodeDirectoryList.clear();
    QStringList attrlst, attrvallst;
    m_ServerModel->ServerDirectory(&ServerDirectoryList);
    xmlAttr<<"inst";
    xmlAttrValue<<rootItem->text(0);
    //m_ModelXml->setRoot("IED",xmlAttr,xmlAttrValue);
    for (int ii = 0; ii < ServerDirectoryList.size(); ii++)
    {
        QTreeWidgetItem *ServerChildItem = new QTreeWidgetItem;
        ServerChildItem->setText(0, ServerDirectoryList.at(ii));
        rootItem->addChild(ServerChildItem);
        GetLogicDeviceDirectory(ServerDirectoryList[ii].toStdString().c_str());
        m_ServerModel->LogicDeviceDirectory(&LogicDeviceDirectoryList);
        xmlAttrValue.clear();
        xmlAttrValue<<ServerDirectoryList[ii];
        //m_ModelXml->AddChildNode("LD",xmlAttr,xmlAttrValue);
        for(int mm = 0; mm < LogicDeviceDirectoryList.size(); mm++)
        {
            QTreeWidgetItem *LDchild = new QTreeWidgetItem;
            LDchild->setText(0, LogicDeviceDirectoryList.at(mm));
            xmlAttrValue.clear();
            xmlAttrValue<<LogicDeviceDirectoryList.at(mm);
            //m_ModelXml->AddChildNode("LN",xmlAttr,xmlAttrValue);
            ServerChildItem->addChild(LDchild);
            m_ServerModel->SetTreeItem(LDchild);
            //m_ServerModel->SetXml(m_ModelXml->GetCurrentNode());
            GetDataDefinition((ServerDirectoryList[ii] + "/" +
                               LogicDeviceDirectoryList[mm]).toStdString().c_str());
            GetDataValues((ServerDirectoryList[ii] + "/" +
                           LogicDeviceDirectoryList[mm]).toStdString().c_str());
            //m_ModelXml->GetParentNode();
        }
        //m_ModelXml->GetParentNode();
        LogicNodeDirectoryList.clear();

        GetLogicNodeDirectory(ServerDirectoryList[ii].toStdString().c_str(),
                              ACSI_DATASET);
        m_ServerModel->LogicNodeDirectory(&LogicNodeDirectoryList);

        for (int ln = 0; ln < LogicNodeDirectoryList.size(); ln++)
        {
            QTreeWidgetItem *Dschild = new QTreeWidgetItem;
            Dschild->setText(0, LogicNodeDirectoryList[ln]);
            ServerChildItem->addChild(Dschild);
            m_ServerModel->SetDataSetItem(Dschild);
            GetDataSetDirectory(LogicNodeDirectoryList[ln].toStdString().c_str());
            GetDataSetValues(LogicNodeDirectoryList[ln].trimmed().toStdString().c_str());
        }
        m_ServerModel->AppSleep(50);
    }
}
BOOL CAcsiAgent::OnGetLogicDeviceDirectory(const char * szLDRef, CMTree & tree)
{
    return m_ServerModel->ProcGetLogicDeviceDirectory(szLDRef, tree);
};
BOOL CAcsiAgent::OnGetLogicNodeDirectory(const char * szLNRef, int iClass,
        CMTree & tree)
{
    return m_ServerModel->ProcGetLogicNodeDirectory(szLNRef, iClass, tree);
};
BOOL CAcsiAgent::OnGetDataValues(const char * szDataRef, CMTree & tree)
{
    return m_ServerModel->ProcGetDataValues(szDataRef, tree);
};
BOOL CAcsiAgent::OnGetDataDirectory(const char * szDataRef, CMTree & tree)
{
    return m_ServerModel->ProcGetDataDirectory(szDataRef, tree);
};
BOOL CAcsiAgent::OnGetDataDefinition(const char * szDataRef, CMTree & tree)
{
    return m_ServerModel->ProcGetDataDefinition(szDataRef, tree);
};
BOOL CAcsiAgent::OnGetDataSetValues(const char * szDataSetRef, CMTree & tree)
{
    return m_ServerModel->ProcGetDataSetValues(szDataSetRef,tree);
};
BOOL CAcsiAgent::OnGetDataSetDirectory(const char * szDataSetRef, CMTree & tree)
{
    return m_ServerModel->ProcGetDataSetDirectory(szDataSetRef, tree);
};
BOOL CAcsiAgent::OnGetBRCB(const char * szBrcbRef, CMTree & tree)
{
    return m_ServiceGetRCB->OnGetRCB(szBrcbRef,tree);
};
BOOL CAcsiAgent::OnGetURCB(const char * szUrcbRef, CMTree & tree)
{
    return m_ServiceGetRCB->OnGetRCB(szUrcbRef,tree);
};
BOOL CAcsiAgent::OnReport(ReportHead * head, AcsiEntry ** entry, int iEntryNum)
{
    return TRUE;
};
BOOL CAcsiAgent::OnLog(BOOL bMorefollow, AcsiEntry ** entry, int iEntryNum)
{
    return TRUE;
};
BOOL CAcsiAgent::OnOperate(OPER_RESULT * oper_result)
{
    return TRUE;
};
BOOL CAcsiAgent::OnGetFileAttrib(const char * szStrName, CMTree & tree,
                                 BOOL morefollow)
{
    printf("OnGetFileAttrib\n");
    return TRUE;
};
BOOL CAcsiAgent::OnGetSGValues(const char * szSGRef, CMTree & tree)
{
    return TRUE;
};
BOOL CAcsiAgent::OnGetSGCBValues(const char * szSGCBRef, CMTree & tree)
{
    return TRUE;
};
